package com.wantup.dubbo;

import com.alibaba.dubbo.rpc.RpcContext;
import com.wantup.dubbo.service.ServiceApi;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Hello world!
 *
 */
public class ConsumerApp
{
    public static void main( String[] args ) throws ExecutionException, InterruptedException {
        //System.out.println( "ConsumerApp --> Hello World!" );
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-hello-consumer.xml");
        context.start();//创建容器 读取配置文件

        while (true){
            Scanner scanner = new Scanner(System.in);
            String msg = scanner.next();

            //获取接口
            ServiceApi serviceApi = (ServiceApi) context.getBean("consumerService");
            //System.out.println(serviceApi.sendMsg(msg));


            //测试异步调用
            long begin = System.currentTimeMillis();

            String sendMsg = serviceApi.sendMsg(msg);
            Future<String> future = RpcContext.getContext().getFuture();

            long end = System.currentTimeMillis();

            String sendMsg02 = serviceApi.sendMsg02(msg);
            Future<String> future02 = RpcContext.getContext().getFuture();




            long end02 = System.currentTimeMillis();

//            System.out.println(" 异步调用sendMsg -->  "+sendMsg);
//            System.out.println(" 异步调用sendMsg02 -->  "+sendMsg02);


            System.out.println(" 异步调用sendMsg 结果-->  " + future.get());
            System.out.println(" 异步调用sendMsg02 结果 -->  " + future02.get());
            System.out.println(" 异步调用sendMsg耗时 -->  "+((end02-begin)));
            System.out.println(" 异步调用sendMsg02耗时 -->  "+((end-begin)));

        }

    }
}
